/**
 * @projectDescription ObjectThought for Javascript
 * 
 * The ObjectThought module establishes ObjectThought's presence
 * in the environment, and provides helpful initialization for 
 * the library, including the load/require infrastructure;
 * borrowed very heavily from the wonderful script.aculo.us ;)
 * 
 * @author Chris Guidry, Jeremy Switzer
 * @version 0.0.1
 * @license MIT
 */
var ObjectThought = {
    
    /**
     * The current version of ObjectThought.
     */
    Version: '0.0.1',
    
    /**
     * The list of libraries to include.  These are all required for the 
     * full feature-set of ObjectThought. 
     */
    DefaultIncludes: ["Validation",
                      "PersistableClass",
                      "ObjectStoreContext",
                      "ObjectStore",
                      "gears/GearsObjectStore",
                      "gears/GearsObjectReader"],
    
    /**
     * Requires a javascript library, by injecting a &lt;script/&gt; tag into the DOM.
     * 
     * @param {String} libraryName
     */
    require: function(libraryName)
    {
        // inserting via DOM fails in Safari 2.0, so brute force approach
        document.write('<script type="text/javascript" src="'+libraryName+'"><\/script>');
    },
    
    /**
     * Indicates which version of <a href="http://prototypejs.org">prototype.js</a> we need.  
     * The world would be quite a terrible place without prototype, and ObjectThought 
     * would be much much more difficult, if not impossible.  Thanks, guys!
     */
    REQUIRED_PROTOTYPE: '1.6.0',
    
    /**
     * Loads ObjectThought and it's dependencies.  It is not necessary to call this method
     * directly, as it's called at the end of ObjectThought.js.
     */
    load: function()
    {
        function convertVersionString(versionString)
        {
          var r = versionString.split('.');
          return parseInt(r[0])*100000 + parseInt(r[1])*1000 + parseInt(r[2]);
        }
        
        if((typeof Prototype=='undefined') || 
           (typeof Element == 'undefined') || 
           (typeof Element.Methods=='undefined') ||
           (convertVersionString(Prototype.Version) < 
            convertVersionString(ObjectThought.REQUIRED_PROTOTYPE)))
           throw("ObjectThought-JS requires the Prototype JavaScript framework >= " + 
                 ObjectThought.REQUIRED_PROTOTYPE);
        
        $A(document.getElementsByTagName("script")).findAll(function(s)
        {
          return (s.src && s.src.match(/ObjectThought\.js(\?.*)?$/))
        }).each(function(s)
        {
            var path = s.src.replace(/ObjectThought\.js(\?.*)?$/, '');
            var includes = s.src.match(/\?.*load=([a-z,]*)/);
            
            includes = (includes ? includes[1].split(',') : ObjectThought.DefaultIncludes)
            
            includes.each(function(include)
            {
                ObjectThought.require(path + include + '.js')
            });
        });
    }
}

ObjectThought.load();